home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ply15dat.zip / SUPERQ.C < prev    next >
C/C++ Source or Header  |  1992-09-07  |  4KB  |  144 lines

  1. /* This program demonstrates a series of morphs of a superquadric */
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include "def.h"
  7. #include "lib.h"
  8.  
  9. static int size_factor = 4;
  10.  
  11. static void
  12. make_superq_file(FILE *outfile, double pow1, double pow2)
  13. {
  14.    COORD4  back_color, obj_color;
  15.    COORD4  backg[4], light;
  16.    COORD4  from, at, up;
  17.    COORD4  center_pt;
  18.    double  lscale, radius, r0, r1;
  19.    int i;
  20.  
  21.    lib_set_output_file(outfile);
  22.    lib_set_raytracer(OUTPUT_POLYRAY);
  23.    lib_set_polygonalization(size_factor, size_factor);
  24.  
  25.    /* set radius of sphere which would enclose entire object */
  26.    radius = 1.5;
  27.  
  28.    /* output viewpoint */
  29.    SET_COORD(from, 1, 5.0, 1.5);
  30.    SET_COORD(at, 0.0, 0.0, 0.0);
  31.    SET_COORD(up, 0.0, 0.0, 1.0);
  32.    lib_output_viewpoint(&from, &at, &up, 45.0, 1.0, 0.01, 128, 128);
  33.  
  34.    SET_COORD(back_color, 0.0, 0.0, 0.0);
  35.    lib_output_background_color(&back_color);
  36.  
  37.    /* output light sources */
  38.    lscale = 1.0 / sqrt(3.0);
  39.    SET_COORD4(light, 4.0, 3.0, 2.0, lscale);
  40.    lib_output_light(&light);
  41.    SET_COORD4(light, 1.0, -4.0, 4.0, lscale);
  42.    lib_output_light(&light);
  43.    SET_COORD4(light, -3.0, 1.0, 5.0, lscale);
  44.    lib_output_light(&light);
  45.  
  46.    /* output floor polygon - beige */
  47.    SET_COORD(back_color, 1.0, 0.75, 0.33);
  48.    lib_output_color(&back_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 1.0);
  49.    r0 =  radius * 20.0;
  50.    r1 = -radius;
  51.    SET_COORD(backg[0],  r0,  r0, r1);
  52.    SET_COORD(backg[1], -r0,  r0, r1);
  53.    SET_COORD(backg[2], -r0, -r0, r1);
  54.    SET_COORD(backg[3],  r0, -r0, r1);
  55.    lib_output_polygon(4, backg);
  56.  
  57.    /* output superquadric at location defined by center */
  58.    SET_COORD(obj_color, 1.0, 0.5, 0.8);
  59.    SET_COORD4(center_pt, 0.0, 0.0, 0.0, 1.0);
  60.    lib_output_color(&obj_color, 0.2, 0.7, 0.0, 0.3, 5.0, 0.0, 1.0);
  61.    lib_output_sq_sphere(¢er_pt, radius, radius, radius, pow1, pow2);
  62. }
  63.  
  64. int
  65. main(int argc, char *argv[])
  66. {
  67.    FILE *outfile;
  68.    int i, cnt = 30;
  69.    double power;
  70.    char txt[64];
  71.  
  72.    /* First loop: turn from a sphere into a octagonal pinchy */
  73.    for (i=0;i<cnt;i++) {
  74.       outfile = fopen("superq.pi", "w");
  75.       power = 1.0 + 2.0 * (double)i / (double)cnt;
  76.       make_superq_file(outfile, power, power);
  77.       fclose(outfile);
  78.       system("polyray superq.pi");
  79.       sprintf(txt, "ren out.tga out%03d.tga", i);
  80.       system(txt);
  81.       }
  82.  
  83.    /* Second loop: turn from a octagonal pinchy into a pinched double cone */
  84.    for (i=0;i<cnt;i++) {
  85.       outfile = fopen("superq.pi", "w");
  86.       power = 3.0 - 2.0 * (double)i / (double)cnt;
  87.       make_superq_file(outfile, 3.0, power);
  88.       fclose(outfile);
  89.       system("polyray superq.pi");
  90.       sprintf(txt, "ren out.tga out%03d.tga", i + 30);
  91.       system(txt);
  92.       }
  93.  
  94.    /* Third loop: turn from a pinched double cone into an cylinder */
  95.    for (i=0;i<cnt;i++) {
  96.       outfile = fopen("superq.pi", "w");
  97.       power = 3.0 - 3.0 * (double)i / (double)cnt;
  98.       make_superq_file(outfile, power, 1.0);
  99.       fclose(outfile);
  100.       system("polyray superq.pi");
  101.       sprintf(txt, "ren out.tga out%03d.tga", i + 60);
  102.       system(txt);
  103.       }
  104.  
  105.    /* Fourth loop: turn from a cylinder into a box */
  106.    for (i=0;i<cnt;i++) {
  107.       outfile = fopen("superq.pi", "w");
  108.       power = 1.0 - (double)i / (double)cnt;
  109.       make_superq_file(outfile, 0.0, power);
  110.       fclose(outfile);
  111.       system("polyray superq.pi");
  112.       sprintf(txt, "ren out.tga out%03d.tga", i + 90);
  113.       system(txt);
  114.       }
  115.  
  116.    /* Fifth loop: turn from a box into a pillow shape */
  117.    for (i=0;i<cnt;i++) {
  118.       outfile = fopen("superq.pi", "w");
  119.       power = (double)i / (double)cnt;
  120.       make_superq_file(outfile, power, 0.0);
  121.       fclose(outfile);
  122.       system("polyray superq.pi");
  123.       sprintf(txt, "ren out.tga out%03d.tga", i + 120);
  124.       system(txt);
  125.       }
  126.  
  127.    /* Sixth loop: turn from a pillow into a sphere */
  128.    for (i=0;i<cnt;i++) {
  129.       outfile = fopen("superq.pi", "w");
  130.       power = (double)i / (double)cnt;
  131.       make_superq_file(outfile, 1.0, power);
  132.       fclose(outfile);
  133.       system("polyray superq.pi");
  134.       sprintf(txt, "ren out.tga out%03d.tga", i + 150);
  135.       system(txt);
  136.       }
  137.  
  138.    /* Now use DTA to assemble the animation */
  139.    system("dta out*.tga");
  140.  
  141.    /* Finally, remove all the Targa files */
  142.    system("del out*.tga");
  143. }
  144.